package hellfire.game.weapons;

import java.util.Vector;

import hellfire.game.Algorithms;
import hellfire.game.Chicken;
import hellfire.game.Game;
import hellfire.platform.graphics.AbstractRenderer;
import hellfire.platform.graphics.BallRenderer;
import hellfire.system.graphics.GLRelVec2;
import hellfire.system.graphics.GLVec2;

public class Ball extends AbstractWeapon
{
	
	private GLVec2 pos;
	private Game game;
	private GLVec2 vel;
	private static GLVec2 dim = new GLVec2(0.05f, 0.05f);
	private BallRenderer renderer;
	
	public Ball(GLVec2 pos, Game game)
	{
		this.pos = pos;
		this.game = game;
		this.vel = new GLVec2(Math.random()-0.5, Math.random()-0.5);
		this.vel = this.vel.normalize().mul(0.03f);
		renderer = new BallRenderer();
	}
	
	
	@Override
	public void gameFrame()
	{
		pos = pos.add(vel);
		
		GLVec2 vel_old = this.vel;
		if(pos.x() + dim.x() > 1) vel = vel.mul(-1, 1);
		if(pos.x() - dim.x() < -1) vel = vel.mul(-1, 1);
		if(pos.y() + dim.y() > 1) vel = vel.mul(1, -1);
		if(pos.y() - dim.y() < -1) vel = vel.mul(1, -1);
		if(vel != vel_old) //directions have changed
			pos = pos.add(vel);
		
		Vector<Object> chickens = game.getObjectsAtPoint(getRenderPos(), Chicken.class, dim.x()+Chicken.getRenderDim().x()/2);
		for(Object c : chickens)
		{
			Chicken chicken = (Chicken) c;
			
			GLVec2[] res = new GLVec2[4];
			Algorithms.ElasticCollision(1, 3, chicken.getVelocity(), this.vel, res);
			
			chicken.setVelocity(res[0]);
			
			chicken.Explode();
		}
		
	}
	
	public GLVec2 getRenderPos()
	{
		return new GLRelVec2(pos);
	}
	
	
	public GLVec2 getPos()
	{
		return this.pos;
	}
	
	public static GLVec2 getDim()
	{
		return dim;
	}

	@Override
	public AbstractRenderer getRenderer() 
	{
		return this.renderer;
	}

}
